# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: jingyuan
import pandas
from coin.exchange.base.order_gateway import OrderSide

class ExportDataFrame(object):
  """export self.errors to dataframe"""

  def __init__(self):
    super().__init__()
    self.errors = {}

  def to_df(self):
    df = pandas.DataFrame.from_dict(self.errors, orient='index').set_index('order_id')
    for f in self.ts_fields:
      df[f] = pandas.to_datetime(df[f])
    return df


class OrderInfoTracker(object):
  """Tracks the basic order information"""
  def __init__(self):
    super().__init__()
    self.order_info = {}
    if hasattr(self, 'ts_fields'):
      self.ts_fields += ['submit_ts']
    else:
      self.ts_fields = ['submit_ts']

  def on_submit(self, ts, symbol, order_id, order_side, order_price, order_qty):
    order_side_to_str = {OrderSide.BUY: 'BUY', OrderSide.SELL: 'SELL', OrderSide.UNKNOWN: 'UNKNOWN'}
    self.order_info[order_id] = {
        'submit_ts': ts,
        'symbol': symbol,
        'side': order_side_to_str.get(order_side, 'UNKNOWN'),
        'price': order_price,
        'qty': order_qty,
    }
